/*
Copyright (c) 2008, Mike Gashler

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include "GQueue.h"
#include <stdlib.h>

GQueueChunk::GQueueChunk(int nSize, const unsigned char* pStaticData)
{
	m_pNext = NULL;
	m_nSize = nSize;
	if(pStaticData)
	{
		m_pData = (unsigned char*)pStaticData;
		m_bDelete = false;
	}
	else
	{
		m_pData = new unsigned char[nSize];
		m_bDelete = true;
	}
}

GQueueChunk::~GQueueChunk()
{
	if(m_bDelete)
		delete[] m_pData;
}

// ----------------------------------------------------------------------------

GQueue::GQueue(int nChunkSize)
{
	nChunkSize = MAX(sizeof(GQueueChunk*), nChunkSize);
	m_nDefaultChunkSize = nChunkSize;
	m_nInPos = nChunkSize;
	m_nOutPos = 0;
	m_pFirstChunk = NULL;
	m_pLastChunk = NULL;
	m_nDataSize = 0;
	m_pExtra = NULL;
}

GQueue::~GQueue()
{
	while(m_pFirstChunk)
		delete(UnlinkFirst());
	delete(m_pExtra);
}

void GQueue::Link(GQueueChunk* pChunk)
{
	if(m_pLastChunk)
	{
		m_pLastChunk->m_pNext = pChunk;
		m_pLastChunk = pChunk;
	}
	else
	{
		m_pFirstChunk = pChunk;
		m_pLastChunk = pChunk;
	}
}

GQueueChunk* GQueue::UnlinkFirst()
{
	GQueueChunk* pChunk = m_pFirstChunk;
	m_pFirstChunk = pChunk->m_pNext;
	if(!m_pFirstChunk)
		m_pLastChunk = NULL;
	pChunk->m_pNext = NULL;
	return pChunk;
}

void GQueue::DropFirstChunk()
{
	delete(m_pExtra);
	m_pExtra = UnlinkFirst();
	m_nOutPos = 0;
}

GQueueChunk* GQueue::MakeNewChunk(int nMinSize)
{
	if(m_pLastChunk)
		m_pLastChunk->m_nSize = m_nInPos;
	if(m_pExtra && m_pExtra->m_bDelete && m_pExtra->m_nSize >= nMinSize)
	{
		Link(m_pExtra);
		m_pExtra = NULL;
	}
	else
	{
		int nSize = MAX(nMinSize, m_nDefaultChunkSize);
		GQueueChunk* pNewChunk = new GQueueChunk(nSize, NULL);
		Link(pNewChunk);
	}
	m_nInPos = 0;
	return m_pLastChunk;
}

void GQueue::Flush()
{
	while(m_pFirstChunk)
		DropFirstChunk();
	m_nInPos = 0;
	m_nOutPos = 0;
	m_nDataSize = 0;
}

void GQueue::PushStatic(const unsigned char* pBuf, int nBufSize)
{
	GQueueChunk* pCurrentChunk = m_pLastChunk;
	if(pCurrentChunk)
		pCurrentChunk->m_nSize = m_nInPos;
	if(m_pExtra && !m_pExtra->m_bDelete)
	{
		pCurrentChunk = m_pExtra;
		m_pExtra = NULL;
		pCurrentChunk->m_pData = (unsigned char*)pBuf;
		pCurrentChunk->m_nSize = nBufSize;
	}
	else
		pCurrentChunk = new GQueueChunk(nBufSize, pBuf);
	Link(pCurrentChunk);
	m_nInPos = nBufSize;
	m_nDataSize += nBufSize;
}

char* GQueue::DumpToString()
{
	int nSize = m_nDataSize;
	char* szStr = new char[nSize + 1];
	Pop((unsigned char*)szStr, nSize);
	szStr[nSize] = '\0';
	return szStr;
}

void GQueue::Poke(int nIndex, const unsigned char* pBuf, int nLen)
{
	while(m_nDataSize < nIndex)
		Push('\0');
	GQueueChunk* pCurrentChunk = m_pFirstChunk;
	while(nIndex >= pCurrentChunk->m_nSize)
	{
		nIndex -= pCurrentChunk->m_nSize;
		pCurrentChunk = pCurrentChunk->m_pNext;
	}
	while(true)
	{
		int nPart = MIN(pCurrentChunk->m_nSize - nIndex, nLen);
		memcpy(&pCurrentChunk->m_pData[nIndex], pBuf, nPart);
		nLen -= nPart;
		if(nLen <= 0)
			return;
        pBuf += nIndex;
		pCurrentChunk = pCurrentChunk->m_pNext;
		nIndex = 0;
	}
}


// ----------------------------------------------------------------------------


GPointerQueueChunk::GPointerQueueChunk(int nChunkSize)
{
	m_pNext = NULL;
	m_pData = new void*[nChunkSize];
	if(!m_pData)
		throw "allocation failed";
}

GPointerQueueChunk::~GPointerQueueChunk()
{
	delete[] m_pData;
}

// ------------------------------------------------------------------------------

GPointerQueue::GPointerQueue(int nChunkSize)
{
	m_nChunkSize = nChunkSize;
	m_nInPos = nChunkSize;
	m_nOutPos = 0;
	m_nDataSize = 0;
	m_pFirstChunk = NULL;
	m_pLastChunk = NULL;
	m_pExtra = NULL;
}

GPointerQueue::~GPointerQueue()
{
	while(m_pFirstChunk)
		delete(UnlinkFirst());
	delete(m_pExtra);
}

void GPointerQueue::Link(GPointerQueueChunk* pChunk)
{
	if(m_pLastChunk)
	{
		m_pLastChunk->m_pNext = pChunk;
		m_pLastChunk = pChunk;
	}
	else
	{
		m_pFirstChunk = pChunk;
		m_pLastChunk = pChunk;
	}
}

GPointerQueueChunk* GPointerQueue::UnlinkFirst()
{
	GPointerQueueChunk* pChunk = m_pFirstChunk;
	m_pFirstChunk = pChunk->m_pNext;
	if(!m_pFirstChunk)
		m_pLastChunk = NULL;
	pChunk->m_pNext = NULL;
	return pChunk;
}

void GPointerQueue::ThrowOutChunk()
{
	if(m_pExtra)
		delete(UnlinkFirst());
	else
		m_pExtra = UnlinkFirst();
	m_nOutPos = 0;
}

void GPointerQueue::Flush()
{
	while(m_pFirstChunk)
		ThrowOutChunk();
	m_nInPos = m_nChunkSize;
	m_nOutPos = 0;
	m_nDataSize = 0;
}

void GPointerQueue::GetNewChunk()
{
	GPointerQueueChunk* pNewChunk = m_pExtra;
	if(!pNewChunk)
		pNewChunk = new GPointerQueueChunk(m_nChunkSize);
	Link(pNewChunk);
	m_pExtra = NULL;
	m_nInPos = 0;
}

// ------------------------------------------------------------------------------

GIntQueue::GIntQueue(int nChunkSize)
{
	m_nChunkSize = nChunkSize;
	m_nInPos = nChunkSize;
	m_nOutPos = 0;
	m_nDataSize = 0;
	m_pFirstChunk = NULL;
	m_pLastChunk = NULL;
	m_pExtra = NULL;
}

GIntQueue::~GIntQueue()
{
	while(m_pFirstChunk)
		delete(UnlinkFirst());
	delete(m_pExtra);
}

void GIntQueue::Link(GPointerQueueChunk* pChunk)
{
	if(m_pLastChunk)
	{
		m_pLastChunk->m_pNext = pChunk;
		m_pLastChunk = pChunk;
	}
	else
	{
		m_pFirstChunk = pChunk;
		m_pLastChunk = pChunk;
	}
}

GPointerQueueChunk* GIntQueue::UnlinkFirst()
{
	GPointerQueueChunk* pChunk = m_pFirstChunk;
	m_pFirstChunk = pChunk->m_pNext;
	if(!m_pFirstChunk)
		m_pLastChunk = NULL;
	pChunk->m_pNext = NULL;
	return pChunk;
}

void GIntQueue::ThrowOutChunk()
{
	if(m_pExtra)
		delete(UnlinkFirst());
	else
		m_pExtra = UnlinkFirst();
	m_nOutPos = 0;
}

void GIntQueue::Flush()
{
	while(m_pFirstChunk)
		ThrowOutChunk();
	m_nInPos = m_nChunkSize;
	m_nOutPos = 0;
	m_nDataSize = 0;
}

void GIntQueue::GetNewChunk()
{
	GPointerQueueChunk* pNewChunk = m_pExtra;
	if(!pNewChunk)
		pNewChunk = new GPointerQueueChunk(m_nChunkSize * sizeof(int) / sizeof(void*));
	Link(pNewChunk);
	m_pExtra = NULL;
	m_nInPos = 0;
}



